package com.ruoyi.web.controller.system;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.dto.AddressDTO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAddress;
import com.ruoyi.system.service.ISysAddressService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 城市字典Controller
 * 
 * @author wendao
 * @date 2024-12-24
 */
@RestController
@RequestMapping("/system/sysAddress")
public class SysAddressController extends BaseController
{
    @Autowired
    private ISysAddressService sysAddressService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询城市字典列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysAddress:list')")
    @GetMapping("/list")
    public AjaxResult list(SysAddress sysAddress)
    {
        String address = redisCache.getCacheObject("addressListAll");

        if(StringUtils.isNotEmpty(address)){
            List<AddressDTO> addressDTOList = JSON.parseArray(address,AddressDTO.class);
            if(addressDTOList.size()>0){
                return AjaxResult.success(addressDTOList);
            }
        }

        List<SysAddress> list = sysAddressService.selectSysAddressList(sysAddress);
        redisCache.setCacheObject("addressListAll",JSON.toJSONString(list));
        return success(list);
    }

    /**
     * 导出城市字典列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysAddress:export')")
    @Log(title = "城市字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAddress sysAddress)
    {
        List<SysAddress> list = sysAddressService.selectSysAddressList(sysAddress);
        ExcelUtil<SysAddress> util = new ExcelUtil<SysAddress>(SysAddress.class);
        util.exportExcel(response, list, "城市字典数据");
    }

    /**
     * 获取城市字典详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysAddress:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(sysAddressService.selectSysAddressById(id));
    }

    /**
     * 新增城市字典
     */
    @PreAuthorize("@ss.hasPermi('system:sysAddress:add')")
    @Log(title = "城市字典", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAddress sysAddress)
    {
        return toAjax(sysAddressService.insertSysAddress(sysAddress));
    }

    /**
     * 修改城市字典
     */
    @PreAuthorize("@ss.hasPermi('system:sysAddress:edit')")
    @Log(title = "城市字典", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAddress sysAddress)
    {
        return toAjax(sysAddressService.updateSysAddress(sysAddress));
    }

    /**
     * 删除城市字典
     */
    @PreAuthorize("@ss.hasPermi('system:sysAddress:remove')")
    @Log(title = "城市字典", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sysAddressService.deleteSysAddressByIds(ids));
    }
}
